﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Domain.MainModule.Entities;
using Infraestructure.Data.MainModule.UnitOfWork;
using Infraestructure.CrossCutting.Logging;
using Domain.MainModule.BillingInformations;
using Infraestructure.Data.MainModule.Repositories;

namespace Infraestructure.Data.MainModule.Tests.RepositoriesTests
{
    [TestClass()]
    public class BillingInformationRepositoryTest : RepositoryTestsBase<BillingInformation>
    {
       public override System.Linq.Expressions.Expression<Func<BillingInformation, bool>> FilterExpression
        {
            get { return a => a.BillingInformationId == TestConstants.Instance.BILLING_INFORMATION_ID; }
        }

        public override System.Linq.Expressions.Expression<Func<BillingInformation, Guid>> OrderByExpression
        {
            get { return a => a.BillingInformationId; }
        }
		
        [TestMethod()]
        [ExpectedException(typeof(ArgumentNullException))]
        public void FindBillingInformation_Invoke_NullSpecThrowNewArgumentNullException_Test()
        {
            //Arrange 
            IMainModuleUnitOfWork context = GetUnitOfWork();
            ITraceManager traceManager = this.GetTraceManager();
            IBillingInformationRepository repository = new BillingInformationRepository(context,traceManager);

            //Act
            repository.FindBillingInformation(null);
        }
			
        #region BillingInformationBillingInformationIdSpecification

        [TestMethod()]
        public void FindBillingInformation_Invoke_InvalidIdReturnNullObject_Test()
        {
            //Arrange 
            IMainModuleUnitOfWork context = GetUnitOfWork();
            ITraceManager traceManager = this.GetTraceManager();
            IBillingInformationRepository repository = new BillingInformationRepository(context, traceManager);

            Guid invalidId = new Guid("11111111-1111-1111-1111-111111111111");
            BillingInformationBillingInformationIdSpecification spec = new BillingInformationBillingInformationIdSpecification(invalidId);

            //Act
            BillingInformation BillingInformation = repository.FindBillingInformation(spec);

            //Assert
            Assert.IsNull(BillingInformation);
        }
		
        [TestMethod()]
        public void FindBillingInformationById_Invoke_Test()
        {
            //Arrange 
            IMainModuleUnitOfWork context = GetUnitOfWork();
            ITraceManager traceManager = this.GetTraceManager();
            IBillingInformationRepository repository = new BillingInformationRepository(context, traceManager);

            BillingInformationBillingInformationIdSpecification spec = new BillingInformationBillingInformationIdSpecification(TestConstants.Instance.BILLING_INFORMATION_ID);

            //Act
            BillingInformation BillingInformation = repository.FindBillingInformation(spec);

            //Assert
            Assert.IsNotNull(BillingInformation);
        }

        #endregion

        #region BillingInformationCompanyNameSpecification
        
        [TestMethod()]
        public void FindBillingInformation_Invoke_InvalidCompanyNameReturnNullObject_Test()
        {
            //Arrange 
            IMainModuleUnitOfWork context = GetUnitOfWork();
            ITraceManager traceManager = this.GetTraceManager();
            IBillingInformationRepository repository = new BillingInformationRepository(context, traceManager);

            string invalidName = "invalid name";
            BillingInformationCompanyNameSpecification spec = new BillingInformationCompanyNameSpecification(invalidName);

            //Act
            IEnumerable<BillingInformation> result = repository.FindBillingInformations(spec);

            //Assert
            Assert.AreEqual(0, result.Count());
        }

        [TestMethod()]
        public void FindBillingInformationByCompanyName_Invoke_Test()
        {
            //Arrange 
            IMainModuleUnitOfWork context = GetUnitOfWork();
            ITraceManager traceManager = this.GetTraceManager();
            IBillingInformationRepository repository = new BillingInformationRepository(context, traceManager);

            string validName = TestConstants.Instance.COMPANY_NAME;
            BillingInformationCompanyNameSpecification spec = new BillingInformationCompanyNameSpecification(validName);

            //Act
            IEnumerable<BillingInformation> result = repository.FindBillingInformations(spec);

            //Assert
            Assert.IsNotNull(result);
            Assert.IsTrue(result.Count() > 0);
        }

        #endregion

        #region BillingInformationCUITSpecification

        [TestMethod()]
        public void FindBillingInformation_Invoke_InvalidCUITReturnNullObject_Test()
        {
            //Arrange 
            IMainModuleUnitOfWork context = GetUnitOfWork();
            ITraceManager traceManager = this.GetTraceManager();
            IBillingInformationRepository repository = new BillingInformationRepository(context, traceManager);

            string invalidCuit = "777";
            BillingInformationCUITSpecification spec = new BillingInformationCUITSpecification(invalidCuit);

            //Act
            IEnumerable<BillingInformation> result = repository.FindBillingInformations(spec);

            //Assert
            Assert.AreEqual(0, result.Count());
        }

        [TestMethod()]
        public void FindBillingInformationByCUIT_Invoke_Test()
        {
            //Arrange 
            IMainModuleUnitOfWork context = GetUnitOfWork();
            ITraceManager traceManager = this.GetTraceManager();
            IBillingInformationRepository repository = new BillingInformationRepository(context, traceManager);

            BillingInformationCUITSpecification spec = new BillingInformationCUITSpecification(TestConstants.Instance.CUIT);

            //Act
            IEnumerable<BillingInformation> result = repository.FindBillingInformations(spec);

            //Assert
            Assert.IsNotNull(result);
            Assert.IsTrue(result.Count() > 0);
        }

        #endregion

        #region BillingInformationGroupBranchIdSpecification

        [TestMethod()]
        public void FindBillingInformation_Invoke_InvalidGroupBranchIdReturnNullObject_Test()
        {
            //Arrange 
            IMainModuleUnitOfWork context = GetUnitOfWork();
            ITraceManager traceManager = this.GetTraceManager();
            IBillingInformationRepository repository = new BillingInformationRepository(context, traceManager);
            
            Guid invalidId = new Guid("11111111-1111-1111-1111-111111111111");
            BillingInformationGroupBranchIdSpecification spec = new BillingInformationGroupBranchIdSpecification(invalidId);

            //Act
            IEnumerable<BillingInformation> result = repository.FindBillingInformations(spec);

            //Assert
            Assert.AreEqual(0, result.Count());
        }

        [TestMethod()]
        public void FindBillingInformationByGroupBranchId_Invoke_Test()
        {
            //Arrange 
            IMainModuleUnitOfWork context = GetUnitOfWork();
            ITraceManager traceManager = this.GetTraceManager();
            IBillingInformationRepository repository = new BillingInformationRepository(context, traceManager);

            BillingInformationGroupBranchIdSpecification spec = new BillingInformationGroupBranchIdSpecification(TestConstants.Instance.GROUP_BRANCH_ID);

            //Act
            IEnumerable<BillingInformation> result = repository.FindBillingInformations(spec);

            //Assert
            Assert.IsNotNull(result);
            Assert.IsTrue(result.Count() > 0);
        }

        #endregion
    }
}
